package com.sywb.chuangyebao.contract;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import com.sywb.chuangyebao.Constants;
import com.sywb.chuangyebao.R;
import com.sywb.chuangyebao.bean.MyArticleBean;
import com.sywb.chuangyebao.utils.DoubleFormatUtil;
import com.sywb.chuangyebao.utils.HttpCallback;
import com.sywb.chuangyebao.utils.HttpUtils;
import com.sywb.chuangyebao.view.NewsDetailActivity;
import com.sywb.chuangyebao.view.PublishArticleActivity;
import com.sywb.chuangyebao.view.PublishVideoEditActivity;
import com.sywb.chuangyebao.view.VideoDetailActivity;
import com.sywb.chuangyebao.view.dialog.AlertDialog;

import org.bining.footstone.adapter.BaseRecyclerAdapter;
import org.bining.footstone.adapter.ViewHolderHelper;
import org.bining.footstone.rxjava.rxbus.RxBus;
import org.bining.footstone.rxjava.rxbus.annotation.Tag;
import org.bining.footstone.rxjava.rxbus.thread.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public interface MyManuscriptContract {

    interface MyManuscriptView extends BaseRefreshContract.View {
        int getTitleStype();

        int getTabStype();
    }

    class MyManuscriptPresenter extends BaseRefreshContract.Presenter<MyManuscriptView> {

        private ManuscriptAdapter articleAdapter = null;
        public int pageNo = 1;
        public int titleStyle;
        private int tabStyle;
        @Override
        public void onStart() {
            super.onStart();
            articleAdapter = new ManuscriptAdapter(mContext);
            titleStyle = mView.getTitleStype();
            tabStyle = mView.getTabStype();
            setRecyclerAdapter(articleAdapter);
            initData();
        }

        @Override
        public void initData() {
            super.initData();
            onStartAsync();
            pageNo = 1;
            listMyArticleOrVideo();
        }

        @Override
        public void onStartRefresh() {
            super.onStartRefresh();
            pageNo = 1;
            listMyArticleOrVideo();
        }

        @Override
        public void onStartLoadMore() {
            super.onStartLoadMore();
            pageNo++;
            listMyArticleOrVideo();
        }

        @Override
        public boolean useRecyclerDivider() {
            return false;
        }
        //点击跳转
        @Override
        public void onRecyclerItemClick(int postion) {
            super.onRecyclerItemClick(postion);
            int status = articleAdapter.getItem(postion).status;
            if (titleStyle == 0) {
                if (status == Constants.ARTICLE_DRAFT || status == Constants.ARTICLE_NOT_PASS) {
                    mView.advance(PublishArticleActivity.class, articleAdapter.getItem(postion));
                }else if (status==Constants.ARTICLE_PASS){
                    mView.advance(NewsDetailActivity.class,articleAdapter.getItem(postion).id);
                }
            } else {
                if (status == Constants.VIDEO_NOT_PASS) {
                    mView.advance(PublishVideoEditActivity.class, articleAdapter.getItem(postion).media_url,2,0,String.valueOf(articleAdapter.getItem(postion).id), articleAdapter.getItem(postion).title);
                }else  if (status==Constants.VIDEO_PASS){
                    mView.advance(VideoDetailActivity.class,articleAdapter.getItem(postion).id);
                }
            }
        }

        //长按
        @Override
        public void onRecyclerItemLongClick(View view, final int postion) {
            super.onRecyclerItemLongClick(view, postion);
            //长按删除 草稿 未通过
            String msg = mContext.getString(R.string.confirm_delete_article);
            String leftBtn = mContext.getString(R.string.text_cancel);
            String rightBtn = mContext.getString(R.string.text_delete);
            int status = articleAdapter.getItem(postion).status;
            if ((titleStyle == 0 && (status == Constants.ARTICLE_DRAFT || status == Constants.ARTICLE_NOT_PASS)) || ((titleStyle == 1 && status == Constants.VIDEO_NOT_PASS))) {
                final AlertDialog dialog = AlertDialog.newInstance(null, msg, leftBtn, rightBtn, false);
                dialog.setClickListener(new AlertDialog.OnClickListener() {
                    @Override
                    public void onClick(int position) {
                        if (position == 0) {
                            dialog.dismiss();
                        } else if (position == 1) {
                            if (titleStyle == 0) {
                                deleteArticle(articleAdapter.getItem(postion).id,postion);
                            } else {
                                deleteVideo(articleAdapter.getItem(postion).id,postion);
                            }
                        }
                    }
                });
                dialog.show(mView.getMyFragmentManager(), "deleteDialog");
            }
        }

        /**
         * 删除文章
         */
        private void deleteArticle(int id, final int position) {
            HttpUtils.deleteArticle(id, new HttpCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    articleAdapter.removeItem(position);
                    articleAdapter.notifyDataSetChanged();
                    RxBus.get().post(Constants.POST_ARTICLE, "");
                }

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }
            });
        }

        //删除视频
        private void deleteVideo(int id, final int position) {
            HttpUtils.deleteVideo(id, new HttpCallback<String>() {
                @Override
                public void onSuccess(String s) {
                    articleAdapter.removeItem(position);
                    articleAdapter.notifyDataSetChanged();
                    RxBus.get().post(Constants.POST_VIDEO_SAVE, "");
                }

                @Override
                public void onStart() {
                    super.onStart();
                    onStartAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    showMessage(strMsg);
                }
            });
        }

        /**
         * 获取我的文章或视频
         */
        public void listMyArticleOrVideo() {
            switch (titleStyle) {
                case 0:
                    if (tabStyle == 0) {
                        getMyArticle(titleStyle, Constants.ARTICLE_ALL);
                    } else if (tabStyle == 1) {
                        getMyArticle(titleStyle, Constants.ARTICLE_DRAFT);
                    } else if (tabStyle == 2) {
                        getMyArticle(titleStyle, Constants.ARTICLE_PENDING);
                    } else if (tabStyle == 3) {
                        getMyArticle(titleStyle, Constants.ARTICLE_PASS);
                    } else if (tabStyle == 4) {
                        getMyArticle(titleStyle, Constants.ARTICLE_NOT_PASS);
                    }
                    break;
                case 1:
                    if (tabStyle == 0) {
                        getMyArticle(titleStyle, Constants.VIDEO_ALL);
                    } else if (tabStyle == 1) {
                        getMyArticle(titleStyle, Constants.VIDEO_PENDING);
                    } else if (tabStyle == 2) {
                        getMyArticle(titleStyle, Constants.VIDEO_PASS);
                    } else if (tabStyle == 3) {
                        getMyArticle(titleStyle, Constants.VIDEO_NOT_PASS);
                    }
                    break;
            }
        }

        private void getMyArticle(int titleStyle, int status) {
            HttpCallback httpCallback = new HttpCallback<List<MyArticleBean>>() {
                @Override
                public void onSuccess(List<MyArticleBean> myArticleBean) {
                    if (myArticleBean != null) {
                        if (pageNo == 1) {
                            articleAdapter.clearDatas();
                            onEndRefresh();
                            if (myArticleBean.size()>0){
                                articleAdapter.setFooterView(R.layout.layout_footer);
                            }else {
                                articleAdapter.setFooterView(null);
                            }
                        }else {
                            if (myArticleBean.size()>=0){
                                articleAdapter.setFooterView(R.layout.layout_footer);
                            }else {
                                articleAdapter.setFooterView(null);
                            }
                        }
                        articleAdapter.notifyDataChangedAfterLoadMore(myArticleBean);
                    }
                }

                @Override
                public void onError(String strMsg) {
                    super.onError(strMsg);
                    if (pageNo == 1) {
                        onEndRefresh();
                    } else {
                        onEndLoadMore();
                    }
                    showMessage(strMsg);
                    onFinishAsync();
                }

                @Override
                public void onFinish() {
                    super.onFinish();
                    onFinishAsync();
                    isNoData();
                }
            };

            if (titleStyle == 0) {
                HttpUtils.myArticle(status, pageNo, httpCallback);
            } else {
                HttpUtils.myVideo(status, pageNo, httpCallback);
            }
        }
        class ManuscriptAdapter extends BaseRecyclerAdapter<MyArticleBean> {
            public ManuscriptAdapter(Context context) {
                super(context, R.layout.item_manuscript_stype);
            }

            @Override
            protected void setItemData(ViewHolderHelper viewHolderHelper, int i, MyArticleBean myArticleBean) {
                myArticleBean.getDateOrTime();
                viewHolderHelper.setText(R.id.item_manuscript_title_tv, myArticleBean.title);
                viewHolderHelper.setText(R.id.item_manuscript_date_tv, myArticleBean.getDate());
                //viewHolderHelper.setText(R.id.item_manuscript_time_tv,myArticleBean.getTime());
                //1表示收入 0表示支出
                if ((myArticleBean.status == Constants.ARTICLE_PASS&&titleStyle==0)||(myArticleBean.status == Constants.VIDEO_PASS&&titleStyle==1)) {
                    viewHolderHelper.setText(R.id.item_manuscript_status_tv, "通过");
                    viewHolderHelper.setTextColor(R.id.item_manuscript_amount_tv, ContextCompat.getColor(mContext, R.color.colorIncome));
                    if (myArticleBean.money==0) {
                        viewHolderHelper.setVisibility(R.id.item_manuscript_amount_tv, false);
                    } else {
                        viewHolderHelper.setVisibility(R.id.item_manuscript_amount_tv, true);
                        viewHolderHelper.setText(R.id.item_manuscript_amount_tv, "+" + DoubleFormatUtil.formatDoubleNumber(myArticleBean.money));
                    }
                } else if (myArticleBean.status == Constants.ARTICLE_DRAFT&&titleStyle==0) {
                    viewHolderHelper.setText(R.id.item_manuscript_status_tv, "草稿");
                    viewHolderHelper.setVisibility(R.id.item_manuscript_amount_tv, false);
                } else if ((myArticleBean.status == Constants.ARTICLE_PENDING&&titleStyle==0)||(myArticleBean.status==Constants.VIDEO_PENDING&&titleStyle==1)) {
                    viewHolderHelper.setText(R.id.item_manuscript_status_tv, "待审核");
                    viewHolderHelper.setVisibility(R.id.item_manuscript_amount_tv, false);
                } else if (myArticleBean.status == Constants.ARTICLE_NOT_PASS||myArticleBean.status==Constants.VIDEO_NOT_PASS) {
                    viewHolderHelper.setText(R.id.item_manuscript_status_tv, "未通过");
                    viewHolderHelper.setVisibility(R.id.item_manuscript_amount_tv, false);
                }
            }
        }
    }
}
